package com.sojson.project.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sojson.project.dao.PostDao;
import com.sojson.project.entity.po.Post;
import com.sojson.project.entity.vo.PostVo;
import com.sojson.project.service.PostService;
import com.sojson.util.file.constant.ConstantFileFieldName;
import com.sojson.util.file.depot.FileDepotUtil;
import com.sojson.util.file.enums.EFileTable;

/**
 * 职位表Service实现类
 * 
 * @author liu
 * @date 2021-04-15
 */
@Service
public class PostServiceImpl implements PostService {

    @Resource
    private PostDao postDaoImpl;

    /**
     * 单条插入
     * 
     * @param post
     * @return
     */
    @Override
    public int insert(Post post) {
        int insert = postDaoImpl.insert(post);

        if (insert > 0) {
            FileDepotUtil.insertSync(EFileTable.POST, ConstantFileFieldName.IMG, post.getImg());
            FileDepotUtil.insertListSync(EFileTable.POST, ConstantFileFieldName.IMGS, post.getImgs());
        }
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param post
     * @return
     */
    @Override
    public int insertList(Post post) {
        int insertList = postDaoImpl.insertList(post.getPosts());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param post
     * @return
     */
    @Override
    public int deleteByDto(Post post) {
        int deleteByDto = postDaoImpl.deleteByDto(post);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @Override
    public int countAll() {
        int countAll = postDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @Override
    public List<Post> findAll() {
        List<Post> findAll = postDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param post
     * @return
     */
    @Override
    public int countByDto(Post post) {
        int countByDto = postDaoImpl.countByDto(post);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param post
     * @return
     */
    @Override
    public List<Post> findByDto(Post post) {
        List<Post> findByDto = postDaoImpl.listByDto(post);
        return findByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param post
     * @return
     */
    @Override
    public List<PostVo> list(Post post) {
        List<PostVo> findByDto = postDaoImpl.list(post);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        int deleteById = postDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @Override
    public int deleteByIdList(List<Integer> ids) {
        int deleteByIdList = postDaoImpl.deleteByIdList(ids);
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param post
     * @return
     */
    @Override
    public int updateById(Post post) {
        int updateById = postDaoImpl.updateById(post);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param post
     * @return
     */
    @Override
    public int updateStaticByIdList(Post post) {
        int updateStaticByIdList = postDaoImpl.updateStaticByIdList(post);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param post
     * @return
     */
    @Override
    public int updateChangeByIdList(Post post) {
        int updateChangeByIdList = postDaoImpl.updateChangeByIdList(post.getPosts());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param post
     * @return
     */
    @Override
    public int updatePartialById(Post post) {
        int updatePartialById = postDaoImpl.updatePartialById(post);
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param post
     * @return
     */
    @Override
    public int updateStaticPartialByIdList(Post post) {
        int updateStaticPartialByIdList = postDaoImpl.updateStaticPartialByIdList(post);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param post
     * @return
     */
    @Override
    public int updateChangePartialByIdList(Post post) {
        int updateChangePartialByIdList = postDaoImpl.updateChangePartialByIdList(post.getPosts());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @Override
    public Post findById(String id) {
        Post findById = postDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     */
    @Override
    public List<Post> findByIdList(List<Integer> ids) {
        List<Post> findByIdList = postDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param post
     * @return
     */
    @Override
    public List<Integer> findIdByDto(Post post) {
        List<Integer> findIdByDto = postDaoImpl.findIdByDto(post);
        return findIdByDto;
    }

    /**
     * 通过userId删除(物理删除)
     * 
     * @param userId
     * @return
     */
    @Override
    public int deleteByUserId(Integer userId) {
        int deleteByUserId = postDaoImpl.deleteByUserId(userId);
        return deleteByUserId;
    }

    /**
     * 通过userId列表删除(物理删除)
     * 
     * @param userIds
     * @return
     */
    @Override
    public int deleteByUserIdList(List<Integer> userIds) {
        int deleteByUserIdList = postDaoImpl.deleteByUserIdList(userIds);
        return deleteByUserIdList;
    }

    /**
     * 通过userId更新(全部更新)
     * 
     * @param post
     * @return
     */
    @Override
    public int updateByUserId(Post post) {
        int updateByUserId = postDaoImpl.updateByUserId(post);
        return updateByUserId;
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容相同)
     * 
     * @param post
     * @return
     */
    @Override
    public int updateStaticByUserIdList(Post post) {
        int updateStaticByUserIdList = postDaoImpl.updateStaticByUserIdList(post);
        return updateStaticByUserIdList;
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容不相同)
     * 
     * @param post
     * @return
     */
    @Override
    public int updateChangeByUserIdList(Post post) {
        int updateChangeByUserIdList = postDaoImpl.updateChangeByUserIdList(post.getPosts());
        return updateChangeByUserIdList;
    }

    /**
     * 通过userId更新(局部更新)
     * 
     * @param post
     * @return
     */
    @Override
    public int updatePartialByUserId(Post post) {
        int updatePartialByUserId = postDaoImpl.updatePartialByUserId(post);
        return updatePartialByUserId;
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容相同)
     * 
     * @param post
     * @return
     */
    @Override
    public int updateStaticPartialByUserIdList(Post post) {
        int updateStaticPartialByUserIdList = postDaoImpl.updateStaticPartialByUserIdList(post);
        return updateStaticPartialByUserIdList;
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容不相同)
     * 
     * @param posts
     * @return
     */
    @Override
    public int updateChangePartialByUserIdList(Post post) {
        int updateChangePartialByUserIdList = postDaoImpl.updateChangePartialByUserIdList(post.getPosts());
        return updateChangePartialByUserIdList;
    }

    /**
     * 通过userId查询
     * 
     * @param userId
     * @return
     */
    @Override
    public Post findByUserId(Integer userId) {
        Post findByUserId = postDaoImpl.findByUserId(userId);
        return findByUserId;
    }

    /**
     * 通过userId列表查询
     * 
     * @param userIds
     * @return
     */
    @Override
    public List<Post> findByUserIdList(List<Integer> userIds) {
        List<Post> findByUserIdList = postDaoImpl.findByUserIdList(userIds);
        return findByUserIdList;
    }

    /**
     * 通过对象查询userId列表
     * 
     * @param post
     * @return
     */
    @Override
    public List<Integer> findUserIdByDto(Post post) {
        List<Integer> findUserIdByDto = postDaoImpl.findUserIdByDto(post);
        return findUserIdByDto;
    }

    /**
     * 通过对象删除(逻辑删除)
     * 
     * @param post
     * @return
     */
    @Override
    public int deleteByDtoAndUpdate(Post post) {
        int deleteByDtoAndUpdate = postDaoImpl.deleteByDtoAndUpdate(post);
        return deleteByDtoAndUpdate;
    }

    /**
     * 统计所有未删除的数据
     * 
     * @return
     */
    @Override
    public int countAllAndStatusNo() {
        int countAllAndStatusNo = postDaoImpl.countAllAndStatusNo();
        return countAllAndStatusNo;
    }

    /**
     * 查询所有未删除的数据
     * 
     * @return
     */
    @Override
    public List<Post> findAllAndStatusNo() {
        List<Post> findAllAndStatusNo = postDaoImpl.findAllAndStatusNo();
        return findAllAndStatusNo;
    }

    /**
     * 通过id删除(逻辑删除)
     * 
     * @param id
     * @return
     */
    @Override
    public int deleteByIdAndUpdate(Integer id) {
        int deleteByIdAndUpdate = postDaoImpl.deleteByIdAndUpdate(id);
        return deleteByIdAndUpdate;
    }

    /**
     * 通过id列表删除(逻辑删除)
     * 
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int deleteByIdListAndUpdate(List<Integer> ids) {
        List<Post> findByIdList = postDaoImpl.findByIdList(ids);

        int deleteByIdListAndUpdate = postDaoImpl.deleteByIdListAndUpdate(ids);

        if (deleteByIdListAndUpdate > 0) {
            List<String> img = findByIdList.stream().map(Post::getImg).collect(Collectors.toList());
            FileDepotUtil.deleteListSync(img);
            List<String> imgs = findByIdList.stream().map(Post::getImgs).collect(Collectors.toList());
            FileDepotUtil.deleteListsSync(imgs);
        }
        return deleteByIdListAndUpdate;
    }

    /**
     * 通过userId删除(逻辑删除)
     * 
     * @param userId
     * @return
     */
    @Override
    public int deleteByUserIdAndUpdate(Integer userId) {
        int deleteByUserIdAndUpdate = postDaoImpl.deleteByUserIdAndUpdate(userId);
        return deleteByUserIdAndUpdate;
    }

    /**
     * 通过userId列表删除(逻辑删除)
     * 
     * @param userIds
     * @return
     */
    @Override
    public int deleteByUserIdListAndUpdate(List<Integer> userIds) {
        int deleteByUserIdListAndUpdate = postDaoImpl.deleteByUserIdListAndUpdate(userIds);
        return deleteByUserIdListAndUpdate;
    }

    /**
     * 清空数据(物理清空)
     */
    @Override
    public void clear() {
        postDaoImpl.clear();
    }

    /**
     * 清空数据(逻辑清空)
     */
    @Override
    public void clearAndUpdate() {
        postDaoImpl.clearAndUpdate();
    }

    /**
     * 根据对象查询(包含附加信息)
     * 
     * @param post
     * @return
     */
    @Override
    public List<Post> findXcxByDto(PostVo post) {
        return postDaoImpl.findXcxByDto(post);
    }

    /**
     * 关闭上岗时间已到的职位
     * 
     * @param thisDate
     * @return
     */
    @Override
    public int closePost(Date thisDate) {
        return postDaoImpl.closePost(thisDate);
    }

    /**
     * Boss首页统计
     * 
     * @return
     */
    @Override
    public Map<String, Integer> countApp(PostVo post) {
        return postDaoImpl.countApp(post);
    }

}